#include<regx51.h>
#define uint unsigned int
#define uchar unsigned char
sbit K1 = P0^ 0;
sbit K2 = P0^ 1;
sbit K3 = P0^ 2;
sbit K4 = P0^ 3;
sbit K5 = P0^ 4;
sbit K6 = P0^ 5;
sbit sound = P3 ^ 6;
uchar hour = 14;
uchar min = 59;
uchar sec = 55;
uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};

void delay(uint i)
{
	int j;
	while (i--)
		for (j = 0; j < 125; j++);
}
void display()
{
	P2=0x01;
	P1=tab[hour/10];
	delay(1);
	P2=0x02;
	P1=tab[hour%10];
	delay(1);
	
	P2=0x04;
	P1=0xbf;
	delay(1);


	P2=0x08;
	P1=tab[min/10];
	delay(1);
	P2=0x10;
	P1=tab[min%10];
	delay(1);
	
	P2=0x20;
	P1=0xbf;
	delay(1);
	
	P2=0x40;
	P1=tab[sec/10];
	delay(1);
	P2=0x80;
	P1=tab[sec%10];
	delay(1);
}


void key()
{
	if (K1 == 0)
		delay(150);
	if(K1==0)
	{
		hour = hour + 1;
		if (hour == 24)
			hour = 0;
	}
	if (K2 == 0)
		delay(150);
	if (K2 == 0)
	{
		hour = hour - 1;
		if (hour == -1)
			hour = 23;
	}
	if (K3 == 0)
		delay(150);
	if (K3 == 0)
	{
		min = min + 1;
		if (min == 60)
			min = 0;
	}
	if (K4 == 0)
		delay(150);
	if (K4 == 0)
	{
		min = min - 1;
		if (min == -1)
			min = 59;
	}
	if (K5 == 0)
		delay(150);
	if (K5 == 0)
	{
		sec = sec + 1;
		if (sec == 60)
			sec = 0;
	}
	if (K6 == 0)
		delay(150);
	if (K6 == 0)
	{
		sec = sec - 1;
		if (sec == -1)
			sec = 59;
	}
}

void main()
{
	EA = 1;
	EX1 = 1;
	IT1=1;

	TMOD = 0x01;
	TH0 = (65536 - 1000) / 256;
	TL0 = (65536 - 1000) % 256;
	TR0 = 1;
	ET0 = 1;

	TMOD = 0x10;
	TH1 = (65536 - 50000) / 256;
	TL1 = (65536 - 50000) % 256;
	TR1 = 1;
	ET1 = 1;


	while(1)
	{
		display();
		key();
	}

}
void zd() interrupt 3
{
	uint t;
	t=t+1;
	if (t == 20)
	{
		sec = sec + 1;
		if (sec == 60)
		{
			sec = 0;
			min = min + 1;
			if (min == 60)
			{
				min = 0;
				hour = hour + 1;
				if (hour == 24)
				{
					hour = 0;
			}
		}
	}
		
	t = 0;
}
	TH1 = (65536 - 50000) / 256;
	TL1 = (65536 - 50000) % 256;

}
void been() interrupt 1
{
	if (hour == 15 && min == 0 && sec == 0)
		sound = ~sound;
	TH0 = (65536 - 1000) / 256;
	TL0 = (65536 - 1000) % 256;
	

}